package edu.zust.aoptest.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class AspectAdvice {
	/**
	 * 前置通知
	 * 
	 * @param jp
	 */
	public void doBefore(JoinPoint jp) {
		System.out.println("===========进入before advice============ \n");

		System.out.print("准备执行" + jp.getTarget().getClass() + " ");
		System.out.print(jp.getSignature().getName() + "('");
		System.out.print(jp.getArgs()[0] + "')方法\n\n");
		System.out.println("===========完成before advice============ \n");
	}
	public void doLog(JoinPoint jp) {
		System.out.println("===========进入before advice============ \n");

		System.out.print("准备执行" + jp.getTarget().getClass() + " ");
		System.out.print(jp.getSignature().getName() + "('");
		System.out.print(jp.getArgs()[0] + "')方法\n\n");
		System.out.println("===========完成before advice============ \n");
	}
	public void doAuth(JoinPoint jp) {
		System.out.println("===========进入before advice============ \n");

		System.out.print("准备执行" + jp.getTarget().getClass() + " ");
		System.out.print(jp.getSignature().getName() + "('");
		System.out.print(jp.getArgs()[0] + "')方法\n\n");
		System.out.println("===========完成before advice============ \n");
	}
	
	/**
	 * 后置通知
	 * 
	 * @param jp
	 *            连接点
	 * @param result
	 *            返回值
	 */
	public void doAfter(JoinPoint jp, String result) {
		System.out.println("+++++++++++++进入after advice+++++++++++++ \n");
		
		System.out.print(jp.getTarget().getClass() + " ");
		System.out.print(jp.getSignature().getName() + "('");
		System.out.print(jp.getArgs()[0] + "')执行完了，");
		
		
		System.out.print("只留下：" + result + "\n\n");
		System.out.println("+++++++++++++完成after advice+++++++++++++ \n");
	}

	/**
	 * 环绕通知
	 * 
	 * @param pjp
	 *            连接点
	 */
	public void doAround(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("$$$$$$$$$$$$$进入around advice！$$$$$$$$$$$$$\n");

		// 调用目标方法之前执行的动作
		System.out.println("调用方法之前: 预备！\n");

		// 调用方法的参数
		Object[] args = pjp.getArgs();
		// 调用的方法名
		String method = pjp.getSignature().getName();
		// 获取目标对象
		Object target = pjp.getTarget();
		// 执行完方法的返回值：调用proceed()方法，就会触发切入点方法执行
		Object result = pjp.proceed();
		

		System.out.println("around："+ target + ";" + method + "('" + args[0] + "'); result:"  + result + "\n");
		System.out.println("调用方法结束：Over！\n");
		System.out.println("$$$$$$$$$$$$$完成around advice！$$$$$$$$$$$$$\n");
	}

	/**
	 * 异常通知
	 * 
	 * @param jp
	 * @param e
	 */
	public void doThrow(JoinPoint jp, Throwable e) {
		System.out.println("删除出错啦");
		System.out.println(e.getMessage());
		
	}

}
